package com.xxxy.quartz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxxy.dao.ExaminationPaperDao;
import com.xxxy.model.ExaminationPaper;
import com.xxxy.utils.QuartzCronDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class QuartzJobListener {

    @Autowired
    QuartzManager quartzManager;

    @Autowired
    ExaminationPaperDao examinationPaperDao;

    public void contextInitialized() {

        //此处模拟从数据库中获取的数据所得到的list
        List<Map<String,Object>> listMap=new ArrayList<>();


        LambdaQueryWrapper<ExaminationPaper> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExaminationPaper::getStatus, 3);
        lambdaQueryWrapper.eq(ExaminationPaper::getTiming, 1);
        List<ExaminationPaper> list = examinationPaperDao.selectList(lambdaQueryWrapper);
        for(int i = 0; i < list.size(); i++){
            try {
                //创建定时任务
                ExaminationPaper examinationPaper = list.get(i);
                quartzManager.addJob("start" + examinationPaper.getId().toString(), "start" +examinationPaper.getId().toString(), "start" +examinationPaper.getId().toString(),"start" +examinationPaper.getId().toString(), StartQuartz.class, QuartzCronDateUtils.getCron(examinationPaper.getExamTime()));
                quartzManager.addJob("end" + examinationPaper.getId().toString(), "end" + examinationPaper.getId().toString(), "end" + examinationPaper.getId().toString(),"end" + examinationPaper.getId().toString(), EndQuartz.class, QuartzCronDateUtils.getCron(examinationPaper.getEndTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
